<?php
declare (strict_types = 1);

namespace app\model;

use app\exception\ModelException;
use app\exception\ModelNotUniqueException;
use think\Model as BaseModel;

/**
 * @mixin \think\Model
 */
class Model extends BaseModel
{
    // msg 
    public $getMsg = '获取成功';
    public $addMsg = '添加成功';
    public $updateMsg = '修改成功';
    public $delMsg = '删除成功';

    // 成功编码
    public $sucCode = 0;
    // 软删除数据
    public $delData;
    // 错误编码
    public $errorCode = '50011';

    protected $hidden = [
        'password','seller_id'
    ];

    public function __construct($data=[])
    {
        $this->delData = [
            'is_del' => 2,
            'delete_time' => time(),
            'update_time' => time(),
        ];
        parent::__construct($data);
    }

    /**
     * @param array $where
     * @return array
     */
    public function getCustomDataColumn(array $where = [],$column):array
    {
        try{
            $res =  $this->where($where)->column($column);
        }catch(\Exception $e){
            return dataReturn($this->errorCode,$e->getMessage());
        }
        return dataReturn($this->sucCode,$this->getMsg,$res);
    }


    /**
     * @param array $where
     * @param int $limit
     * @return array
     */
    public function getCustomDataList(array $where = [],int $limit = 10):array
    {
        try{
            $res =  $this->where($where)->limit($limit)->paginate();
        }catch(\Exception $e){
            return dataReturn($this->errorCode,$e->getMessage());
        }
        return dataReturn($this->sucCode,$this->getMsg,$res);
    }

    public function getAllCustomData($where,$order='id desc',$field="*",$with=[]): array
    {
        try{
            $res =  $this->field($field)->with($with)->where($where)->order($order)->select()->toArray();
        }catch(\Exception $e){
            return dataReturn($this->errorCode,$e->getMessage());
        }
        return dataReturn($this->sucCode,$this->getMsg,$res);
    }

    /**
     * @param array $name
     * @return array
     */
    public function getCustomData(array $name = [],$with=[],$field="*"): array
    {
        try{
            $res =  $this->with($with)->field($field)->where($name)->find();
        }catch(\Exception $e){
            return dataReturn($this->errorCode,$e->getMessage());
        }
        return dataReturn($this->sucCode,$this->getMsg,$res);

    }

    /**
     * @param $param
     * @return array
     */
    public function addCustomData($param): array
    {
        try{
            $res =  self::create($param);
        }catch(\Exception $e){
            return dataReturn($this->errorCode,$e->getMessage());
        }
        return dataReturn($this->sucCode,$this->addMsg,$res);
    }

    /**
     * @param $param
     * @return array
     */
    public function addAllCustomData($param): array
    {
        try{
            $res =  self::saveAll($param);
        }catch(\Exception $e){
            return dataReturn($this->errorCode,$e->getMessage());
        }
        return dataReturn($this->sucCode,$this->addMsg,$res);
    }

    /**
     * @param array $where
     * @param $param
     * @return array
     */
    public function updateCustomData(array $where, $param,$strict = true): array
    {
        try{
            $res =  self::strict($strict)->where($where)->update($param);
        }catch(\Exception $e){
            return dataReturn($this->errorCode,$e->getMessage());
        }
        return dataReturn($this->sucCode,$this->updateMsg,$res);
    }

    /**
     * @param $where
     * @return array
     */
    public function delCustomData($where): array
    {
        try{
            $res =  self::where($where)->delete();
        }catch(\Exception $e){
            return dataReturn($this->errorCode,$e->getMessage());
        }
        return dataReturn($this->sucCode,$this->delMsg,$res);
    }
    /**
     * @throws ModelNotUniqueException
     * @throws ModelException
     */
    public function saveUnique($where, $msg = '内容已存在')
    {
        try{
            $res = $this->where($where)->find();
        }catch(\Exception $e){
            throw new ModelException($e->getMessage());
        }
        if(!empty($res)){
            throw new ModelNotUniqueException($msg);
        }
    }

    /**
     * @throws ModelException
     * @throws ModelNotUniqueException
     */
    public function updateUnique($where, $id, $msg = '内容已存在')
    {
        try{
            $res = $this->where($where)->find();
        }catch(\Exception $e){
            throw new ModelException($e->getMessage());
        }
        if(!empty($res) && $res['id'] != $id){
            throw new ModelNotUniqueException($msg);
        }
    }

}
